﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Lawyer.Web.Admin
{
    public partial class admin_userupdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ID = Request["id"] != null ? Request["id"].ToString() : "";
            if (ID == "")
            {
                Response.Redirect("admin_userlist.aspx");
            }
            if (!IsPostBack)
            {
                BindUserInfo(ID);
            }

        }

        public void BindUserInfo(string ID)
        {
            Lawyer.BLL.UserInfo bll = new Lawyer.BLL.UserInfo();
            Lawyer.Model.UserInfo model = bll.GetModelFromUserID(ID);
            if (model == null) {
                Response.Redirect("admin_userlist.aspx");
            }
            txbBirthday.Text = model.Birthday == null ? "" : model.Birthday.Value.ToShortDateString();
            txbEmail.Text = model.Email;
            txbFullName.Text = model.FullName;
            txbUserName.Text = model.UserName;
            txtAddress.Text = model.Address;
            txtAvatarURL.Text = model.AvatarURL;
            txtCity.Text = model.City;
            txtFax.Text = model.Fax;
            txtTelphone.Text = model.Telphone;
            rblGender.SelectedValue = model.Sex;

        }

        private void CreateFromForm(Lawyer.Model.UserInfo model)
        {
            string UserName = this.txbUserName.Text;
           
            string FullName = this.txbFullName.Text;
            string Email = this.txbEmail.Text;
            string Sex = this.rblGender.SelectedValue;
            string City = this.txtCity.Text;
            string Telphone = this.txtTelphone.Text;
            string Fax = this.txtFax.Text;
            string Address = this.txtAddress.Text;
            string AvatarURL = this.txtAvatarURL.Text;

            model.UserName = UserName;
            model.FullName = FullName;
            model.Email = Email;
            model.Sex = Sex;
            try
            {
                model.Birthday = DateTime.Parse(this.txbBirthday.Text); ;
            }
            catch {
                model.Birthday = null;
            }
            model.City = City;
            model.Telphone = Telphone;
            model.Fax = Fax;
            model.Address = Address;
            model.AvatarURL = AvatarURL;

            model.Status = 0;//0=>Normal,1=>Deleted
            //model.Type = 1;//1=>Lawyer,0=>admin
            model.UpdatedBy = Session["UserID"].ToString(); ;
            model.UpdatedDate = DateTime.Now;

        }
        private string InitFromUrl(string url)
        {
            string rtn = Request[url] != null ? Request[url] : "";
            return rtn;
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string ID = InitFromUrl("ID");
            
            Lawyer.BLL.UserInfo BLL = new Lawyer.BLL.UserInfo();
            Lawyer.Model.UserInfo model = BLL.GetModelFromUserID(ID);
            if (model == null) {
                Response.Redirect("admin_userlist.aspx");
            }
            CreateFromForm(model);

            if (BLL.Update(model))
            {
                Maticsoft.Common.MessageBox.Show(this, "Success！");
            }
            else {
                Maticsoft.Common.MessageBox.Show(this, "Failed！");
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("admin_userlist.aspx");
        }
    }
}
